package xy.donate.android.ui.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.Button
import androidx.room.Room
import com.hjq.base.BaseDialog
import kotlinx.android.synthetic.main.activity_commit_order.*
import xy.donate.android.R
import xy.donate.android.common.MyActivity
import xy.donate.android.constant.Constant
import xy.donate.android.db.AddressDb
import xy.donate.android.db.OrderDb
import xy.donate.android.entity.AddressEntity
import xy.donate.android.entity.OrderBean
import xy.donate.android.helper.MmkvHelper
import xy.donate.android.helper.TimeUtils
import xy.donate.android.ui.dialog.DateDialog
import xy.donate.android.ui.dialog.InputDialog
import xy.donate.android.ui.dialog.TimeDialog
import java.util.*

class CommitOrderActivity : MyActivity() {

    private var addressEntity: AddressEntity? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_commit_order
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {

        val address = Room.databaseBuilder(this, AddressDb::class.java, "addressDB")
                .allowMainThreadQueries()
                .build().addressDao()

        val dao = Room.databaseBuilder(this, OrderDb::class.java, "orderDB")
                .allowMainThreadQueries()
                .build().orderDao()

        ll_select_time.setOnClickListener {
            // 日期选择对话框
            DateDialog.Builder(this)
                    .setTitle(getString(R.string.date_title)) // 确定按钮文本
                    .setConfirm(getString(R.string.common_confirm)) // 设置 null 表示不显示取消按钮
                    .setCancel(getString(R.string.common_cancel)) // 设置日期
                    .setDate(Calendar.getInstance().timeInMillis)
                    .setListener { dialog, year, month, day ->
                        // 时间选择对话框
                        TimeDialog.Builder(this@CommitOrderActivity)
                                .setTitle(getString(R.string.time_title)) // 确定按钮文本
                                .setConfirm(getString(R.string.common_confirm)) // 设置 null 表示不显示取消按钮
                                .setCancel(getString(R.string.common_cancel)) // 设置时间
                                .setIgnoreSecond()
                                .setListener { dialog1, hour, minute, _ ->
                                    tv_time.text = "$year-$month-$day $hour:$minute"
                                    dialog1.dismiss()
                                }
                                .show()
                        dialog.dismiss()
                    }
                    .show()
        }

        val list = address.getAll(MmkvHelper.getInstance().mmkv.getString(Constant.USER_ID_NUM, ""))
        if (!list.isNullOrEmpty()) {
            ll_address.visibility = View.VISIBLE
            tv_name_hint.visibility = View.GONE
            addressEntity = list[0]
            tv_name_phone.text = "${addressEntity?.realName}     ${addressEntity?.phoneNum}"
            tv_order_address.text = addressEntity?.detailAddress
        } else {
            ll_address.visibility = View.GONE
            tv_name_hint.visibility = View.VISIBLE
        }

        tv_name_hint.setOnClickListener {
            val intent = Intent(this, AddressActivity::class.java)
            intent.putExtra("order", 1)
            startActivityForResult(intent, 888)
        }

        ll_address.setOnClickListener {
            val intent = Intent(this, AddressActivity::class.java)
            intent.putExtra("order", 1)
            startActivityForResult(intent, 888)
        }
        ll_bz.setOnClickListener {
            // 备注输入
            InputDialog.Builder(this) // 标题可以不用填写
                    .setTitle("请输入") // 内容可以不用填写
                    .setContent(tv_bz.text)
                    .setHint("如需留言请备注") // 提示可以不用填写
                    .setConfirm(getString(R.string.common_confirm)) // 设置 null 表示不显示取消按钮
                    .setCancel(getString(R.string.common_cancel)) // 设置点击按钮后不关闭对话框
                    .setListener { _, content ->
                        tv_bz.text = content
                    }
                    .show()
        }

        ll_commit_order.setOnClickListener {
            val order = OrderBean()
            order.address = addressEntity!!
            if (!TextUtils.isEmpty(tv_bz.text)) {
                order.bz = tv_bz.text.toString()
            }
            if (tv_name_hint.visibility == View.VISIBLE) {
                toast("请选择地址信息")
                return@setOnClickListener
            }
            order.userID = MmkvHelper.getInstance().mmkv.getString(Constant.USER_ID_NUM, "")
            order.time = tv_time.text.toString()
            order.orderNo = Constant.getOrderNo()
            order.orderTime = TimeUtils.getCurTimeString()
            dao.insert(order)
            BaseDialog.Builder<BaseDialog.Builder<*>>(this)
                    .setContentView(R.layout.custom_dialog)
                    .setAnimStyle(BaseDialog.ANIM_SCALE) //.setText(id, "我是预设置的文本")
                    .setOnClickListener(R.id.btn_dialog_custom_ok, BaseDialog.OnClickListener<Button?> { dialog: BaseDialog, view: Button? -> dialog.dismiss() })
                    .addOnDismissListener {
                        val intent = Intent(this, OrderDetailActivity::class.java)
                        intent.putExtra("OrderNo", order.orderNo)
                        startActivity(intent)
                        finish()
                    }
                    .show()
        }
    }

    override fun initData() {

    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 888 && resultCode == Activity.RESULT_OK && data != null) {
            ll_address.visibility = View.VISIBLE
            tv_name_hint.visibility = View.GONE
            val address = data.getSerializableExtra("address") as AddressEntity
            addressEntity = address
            tv_name_phone.text = "${address.realName}     ${address.phoneNum}"
            tv_order_address.text = address.detailAddress
        } else {
            ll_address.visibility = View.GONE
            tv_name_hint.visibility = View.VISIBLE
        }
    }
}